function [tab]= createTableResults(dasXML,parentFig)
% CREATETABLERESULTS creates a JTable with the results of searching in DAS Registry
% 
% SYNTAX: [tab]= createTableResults(dasXML,parentFig)
%          dasXML - DAS XML from the server
%          parentFig - handle for the parent graphical object
%          tab - handle for the newly created uitable 
%
% See also SEARCHREGISTRY_GUI
%  
%    DASMiner: DAS library and browser for Matlab.
%    Diogo Veiga, March 2008.


try 
    t_parseany = xml_parseany(dasXML);
catch
    msgbox('Problem Parsing XML DAS Response','Bad Parsing','warn');
    tab = -1;
    return;
end

if (~isfield(t_parseany,'SOURCE')) %No matches
   h = msgbox('No Data sources found.','Query results','warn');
   movegui(h,'onscreen');
   tab = -1;
   return;
end

tab = uitable(size(t_parseany.SOURCE,2),5,parentFig);
set(tab,'Visible',0);
set(tab,'ColumnNames',{'ID', 'Title', 'Description', 'Capabilities','Source Details'});
set(tab,'Position',[20 20 675.45 200]);
tab.setVisible(0);
% Multiple selection intervals can be made
tab.Table.setSelectionMode(tab.Table.getSelectionModel.MULTIPLE_INTERVAL_SELECTION);

%jt.AUTO_RESIZE_ALL_COLUMNS;
%tModel.setValueAt('oi',2,2); %setValueAt(Object value, int row, int col)
%table = tab.getTable; %physical attributes

tModel = tab.getTableModel; %actual data
for i=1:size(t_parseany.SOURCE,2)
   
    tModel.setValueAt(t_parseany.SOURCE{i}.ATTRIBUTE.uri,i-1,0);
    tModel.setValueAt(t_parseany.SOURCE{i}.ATTRIBUTE.title,i-1,1);
    tModel.setValueAt(t_parseany.SOURCE{i}.ATTRIBUTE.description,i-1,2);
    
    if (isfield(t_parseany.SOURCE{i}.VERSION{1},'CAPABILITY'))

        tokens = regexp(t_parseany.SOURCE{i}.VERSION{1}.CAPABILITY{1}.ATTRIBUTE.type,':','split');
        capabilities = tokens{2};
    
        for k=2:size(t_parseany.SOURCE{i}.VERSION{1}.CAPABILITY,2)
            tokens = regexp(t_parseany.SOURCE{i}.VERSION{1}.CAPABILITY{k}.ATTRIBUTE.type,':','split');
            capabilities = strcat(capabilities,',',tokens{2});
        end
    end

    tModel.setValueAt(capabilities,i-1,3);
    tModel.setValueAt(strcat('http://www.dasregistry.org/showdetails.jsp?auto_id=',t_parseany.SOURCE{i}.ATTRIBUTE.uri),i-1,4);
    
end

tab.setRowHeight(20);
tab.setColumnWidth(170);

